import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Index from '../views/Index.vue'

// 药店
import ToDrugInfo from '../views/drugstore/ToDrugInfo.vue'
import DrugAdmin from '../views/drugstore/drug/DrugAdmin.vue'
import drugClassify from '../views/drugstore/drug/drugClassify.vue'
import orderList from '../views/drugstore/orderAdmin/orderList.vue'

// 自己的平台
import ToAudit from '../views/selfTerrace/audit/ToAudit.vue'
//个人中心
import userInfo from '../views/userInfo/index'
//运营 banner管理 权限菜单
import toBanner from '../views/banner/toBanner'
import toRoleMenu from '../views/banner/toRoleMenu'
import toMenu from '../views/banner/toMenu'
Vue.use(VueRouter)
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error)
}

const routes = [
    { path: '/', name: 'Login', component: Login },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        redirect: '/index',
        children: [
            { path: '/index', name: '工作台', component: Index },
            { path: '/toAudit', name: '入驻平台', component: ToAudit },
            { path: '/toDrugInfo', name: '药店信息', component: ToDrugInfo },
            { path: '/drugClassify', name: '分类管理', component: drugClassify },
            { path: '/drugAdmin', name: '药品管理', component: DrugAdmin },
            { path: '/orderList', name: '订单列表', component: orderList },
            { path: '/userInfo', name: '个人中心', component: userInfo },
            { path: '/toBanner', name: 'banner管理', component: toBanner },
            { path: '/toRoleMenu', name: '权限菜单', component: toRoleMenu },
            { path: '/toMenu', name: '菜单管理', component: toMenu },
        ]
    },
]
const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    let sessionId = sessionStorage.getItem("sessionId");
    if (to.name != 'Login') {
        if (sessionId) {
            next();
        } else {
            next({ path: '/' })
        }
    } else {
        // sessionStorage.clear();
        localStorage.clear();
        next();
    }
})

export default router